package com.baiyang.app;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.model.User;
import com.baiyang.app.service.WebSocketService;
import com.baiyang.app.tool.net.MyWebSocketSendUtil;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import com.baiyang.app.databinding.ActivityHomeBinding;


import java.util.HashMap;
import java.util.Map;

/**
 * fragment 跳转动画的实现
 * 1.action的声明
 * 2.setOnClickListener执行action
 */
public class HomeActivity extends AppCompatActivity {
    private ActivityHomeBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        // 绑定service
        bindService();
        // 注册广播
        IntentFilter itFilter = new IntentFilter();
        itFilter.addAction("com.baiyang.MESSAGE");
        itFilter.setPriority(0);
        registerReceiver(broadcastReceiver, itFilter);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_home);
        NavigationUI.setupWithNavController(navView, navController);
        findViewById(R.id.book_city).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.findNavController(HomeActivity.this, R.id.nav_host_fragment_activity_home).navigate(R.id.action_global);
            }
        });
    }
    private WebSocketService.serviceBinder binder;
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            binder = null;
            System.out.println(name);
            Log.i("disCounnect-->",name.toString());
        }
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("Counnect-->",name.toString());
            binder = (WebSocketService.serviceBinder) service;
            MyWebSocketSendUtil.setListener(binder.getMyWebSocketListener());
            //登陆成功后开启websocket服务
            User user = MyApp.getInstance().getUser();
            if(user != null){
                MyWebSocketSendUtil.setUrl(user.getId());
                MyWebSocketSendUtil.getWebSocket();
            }
        }
    };
    private void bindService() {
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(),WebSocketService.class);
        bindService(intent, conn, Service.BIND_AUTO_CREATE);
    }
    private BroadcastReceiver broadcastReceiver =  new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Map<String,String> map = new HashMap<>();
            Map msg = (Map) intent.getSerializableExtra("message");
            StringBuilder mes = new StringBuilder();
            mes.append("作者：");
            mes.append(msg.get("authorName").toString() + "\n");
            mes.append("发布文章：");
            mes.append(msg.get("articleName").toString() + "\n");
            Toast.makeText(getApplicationContext(),mes.toString(),Toast.LENGTH_SHORT).show();
            //停止发送
            abortBroadcast();
        }
    };
}